$(function () {
    obj.init();
});

var obj = new function() {
    return {
        form: null,
        dom: {
            extraPower: null
        },
        init: function () {
            this.dom.extraPower = $('.power-extra:first').clone();

            this.form = new xhForm($('#form_power'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            //收缩
            $('.tree-view-menu-list').on('click', '.openable a', function()	{
                var parentElm = $(this).parent();
                parentElm.toggleClass('open');
                parentElm.children('.subtree').slideToggle(200);
                return false;
            }).on('click', 'a', function () {
                $this.funObj().readNodeInfo($(this).attr('nodeId'));
            });
            //增加其他权限
            $('#bt_addExtra').click(function () {
                $this.funObj().addExtraPower();
            });
            //重置
            $('#bt_reset').click(function () {
                $this.funObj().reset();
            });
            //生成基本权限
            $('#bt_createBase').click(function () {
                $this.funObj().createBase();
            });
            //提交权限
            $('#bt_confirm').click(function () {
                $this.funObj().submitPower();
            });
            //查看权限存在用户
            $this.form.form.on('click', '.view-exist', function () {
                $this.funObj().viewExistUser($(this).siblings('input:eq(0)').val());
            })
        },
        funObj: function () {
            var $this = this;
            return {
                readNodeInfo: function (nodeId) {
                    $this.form.reset();
                    $('.power-extra:gt(0)').remove();
                    if (!nodeId) {
                        return false;
                    }
                    xhExtra.ajaxRequest('sys/readPowerInfo', {id: nodeId}, {check: true}, function (data) {
                        $this.form.fill(data);
                        $this.funObj().initExtraPower(data.extra);
                    });
                },
                addExtraPower: function (power) {
                    var powerStr = $this.dom.extraPower.clone();
                    if (power) {
                        powerStr.find('input[name="powerTag[]"]').val(power.powerTag);
                        powerStr.find('input[name="powerName[]"]').val(power.powerName);
                    }
                    $('#bt_addExtra').before(powerStr);
                },
                initExtraPower: function (powers) {
                    $('.power-extra').remove();
                    if (powers){
                        $.each(powers, function (index, power) {
                            $this.funObj().addExtraPower(power);
                        });
                    }
                    $this.funObj().addExtraPower();
                },
                reset: function () {
                    $this.form.reset();
                    $this.funObj().initExtraPower();
                },
                createBase: function () {
                    var basePower = $('#mainPowerTag').val();
                    if (!basePower) {
                        window.top.xhTop.showError('请输入主权限！');
                        return false;
                    }
                    $('#mainPowerName').val('查看');
                    $this.funObj().initExtraPower([
                        {powerTag: basePower + '_Add', powerName: '增加'},
                        {powerTag: basePower + '_Edit', powerName: '修改'},
                        {powerTag: basePower + '_Del', powerName: '删除'}
                    ]);
                },
                submitPower: function () {
                    $this.form.submit('sys/savePower', {}, function (result) {
                        xhExtra.setBtnNormal($('#bt_confirm'));
                        if (xhExtra.checkError(result)) return false;
                        window.top.xhTop.showSuccess(result.msg);
                    }, {check: false});
                },
                viewExistUser: function (tag) {
                    if (!tag) {
                        window.top.xhTop.showError('权限不可为空！');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '已存在该权限用户-' + tag,
                        content: xhTool.getFrameString('power/existPowerUser', {powerTag: tag}, 400)
                    });
                }
            }
        }
    }
};